package com.jiang.jixiang.ui.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiang.jixiang.R;
import com.jiang.jixiang.api.ApiUrl;
import com.jiang.jixiang.databinding.FragmentInviteBinding;
import com.jiang.jixiang.network.CommonalityModel;
import com.jiang.jixiang.network.NetWorkListener;
import com.jiang.jixiang.network.okHttpModel;
import com.ws.universal.tools.base.BaseFragment;
import com.ws.universal.tools.utils.FileUtils;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONObject;

/**
 * 邀请好友
 */
public class InviteFragment extends BaseFragment implements NetWorkListener {
    private FragmentInviteBinding binding;


    @Override
    public View getRootView(LayoutInflater inflater, ViewGroup container) {
        binding = FragmentInviteBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void initView() {
        binding.inTitle.titleLeftBtn.setVisibility(View.GONE);
        binding.inTitle.titleTextTv.setText(R.string.invite_friends);
        binding.btnCopy.setChangeAlphaWhenPress(true);
        binding.btnCopy.setOnClickListener(view -> {
            FileUtils.copy(getActivity(), "invaterCode", binding.tvInviterCode.getText().toString(), getString(R.string.copy_success));
        });
        loadData();
    }

    @Override
    public void autoData() {

    }

    private void loadData() {
        showProgressDialog(getContext(), false);
        okHttpModel.post(ApiUrl.invitationDetail_Api, null, ApiUrl.invitationDetail_Api_ID, this);
    }

    @Override
    public void onSucceed(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        JSONObject data = jsonData.optJSONObject("data");
        switch (id) {
            case ApiUrl.invitationDetail_Api_ID:
                binding.tvInviterCode.setText(data.optString("inviter_code"));
                binding.tvTime.setText(data.optString("time"));
                binding.tvNum.setText(data.optString("num"));
                break;
        }


    }

    @Override
    public void onFail(JSONObject jsonData, int id, CommonalityModel commonality) {
        stopProgressDialog();
        ToastUtils.showShort(commonality.getMessage());
    }

    @Override
    public void onError(int id, Exception e) {
        stopProgressDialog();
        ToastUtils.showShort(e.getMessage());
    }

}
